package com.feng.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feng.chat.dto.ClassListDTO;
import com.feng.chat.dto.Result;
import com.feng.chat.dto.TagListDTO;
import com.feng.chat.entity.Article;
import com.feng.chat.entity.ArticleClass;
import com.feng.chat.entity.ArticleTag;
import com.feng.chat.mapper.ArticleClassMapper;
import com.feng.chat.mapper.ArticleMapper;
import com.feng.chat.mapper.ArticleTagMapper;
import com.feng.chat.service.IArticleClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.chat.vo.ClassItemVO;
import com.feng.chat.vo.PaginateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 笔记分类表 服务实现类
 * </p>
 *
 * @author dyf
 * @since 2022-05-31
 */
@Service
public class ArticleClassServiceImpl extends ServiceImpl<ArticleClassMapper, ArticleClass> implements IArticleClassService {

    @Autowired
    private ArticleClassMapper articleClassMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleTagMapper articleTagMapper;

    /**
     * 获取笔记分类列表
     *
     * @param uid
     * @return
     */
    @Override
    public Result classList(Integer uid) {
        QueryWrapper<ArticleClass> articleClassQueryWrapper = new QueryWrapper<>();
        articleClassQueryWrapper.eq("user_id", uid).eq("is_delete", 0).orderByAsc("sort");
        List<ArticleClass> articleClasses = articleClassMapper.selectList(articleClassQueryWrapper);
        List<ClassItemVO> classItemVOS = new ArrayList<>();
        classItemVOS = articleClasses.stream().map(item -> {
            ClassItemVO classItemVO = new ClassItemVO();
            classItemVO.setClass_name(item.getClassName());
            classItemVO.setId(item.getId());
            QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
            articleQueryWrapper.eq("class_id", item.getId()).eq("user_id", item.getUserId()).eq("status", 1);
            Long count = articleMapper.selectCount(articleQueryWrapper);
            classItemVO.setCount(count);
            classItemVO.setIs_default(item.getIsDefault());
            return classItemVO;
        }).collect(Collectors.toList());

        PaginateVO paginateVO = new PaginateVO();
        paginateVO.setSize(1000);
        paginateVO.setPage(1);
        paginateVO.setTotal(classItemVOS.size());

        ClassListDTO classListDTO = new ClassListDTO();
        classListDTO.setRows(classItemVOS);
        classListDTO.setPaginate(paginateVO);
        return Result.success(classListDTO);
    }

    /**
     * 编辑分类
     *
     * @param uid
     * @param class_id
     * @param class_name
     * @return
     */
    @Override
    public Result classEditor(Integer uid, Integer class_id, String class_name) {
        Map<String, Integer> map = new HashMap<>();
        if (class_id == 0) {//新建分类
            QueryWrapper<ArticleClass> articleClassQueryWrapper = new QueryWrapper<>();
            articleClassQueryWrapper.eq("user_id",uid).eq("class_name",class_name);
            ArticleClass articleClass1 = articleClassMapper.selectOne(articleClassQueryWrapper);
            if (articleClass1!=null){
                if (articleClass1.getIsDelete()==1){
                    articleClass1.setIsDelete(0);
                    articleClassMapper.updateById(articleClass1);

                    map.put("id",articleClass1.getId());
                    return Result.success(map);
                }
                return Result.fail("该分类已存在！！！");
            }
            ArticleClass articleClass = new ArticleClass();
            articleClass.setClassName(class_name);
            articleClass.setUserId(uid);

            articleClassMapper.insert(articleClass);

            map.put("id",articleClass.getId());

            return Result.success(map);
        } else {//编辑分类
            ArticleClass articleClass = articleClassMapper.selectById(class_id);
            articleClass.setClassName(class_name);
            articleClassMapper.updateById(articleClass);

            map.put("id",articleClass.getId());
            return Result.success(map);
        }
    }

    /**
     * 删除分类
     * @param uid
     * @param class_id
     * @return
     */
    @Override
    public Result classDelete(Integer uid, Integer class_id) {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("user_id",uid).eq("class_id",class_id).eq("status",1);
        List<Article> articles = articleMapper.selectList(articleQueryWrapper);
        if (articles.size()>0){
            return Result.fail("当前分类下含有笔记，无法删除！");
        }
        ArticleClass articleClass = articleClassMapper.selectById(class_id);
        articleClass.setIsDelete(1);
        articleClassMapper.updateById(articleClass);
        return Result.success();
    }
}
